'use strict';
var React = require('react');
var reactDom = require('react-dom/server');
var reactRedux = require('react-redux');
var configureStore = require('../public/javascripts/stores/index.js');
var HkfsProductDetail = require('../public/javascripts/containers/pc/HkfsProductDetail.jsx');
var Provider = reactRedux.Provider;
const store = configureStore();
var ModelRender = require('./modelRender.jsx');
var actions = require('../public/javascripts/actions/productDetail');
var async = require('async');
var HttpOperate = require('../nodeLibrary/HttpOperate');
var httpOperate = new HttpOperate();
var getListByparId = require('../nodeLibrary/dataOperate/getListByParId');
var logger = require('../app').logger('userMsg');

exports.RenderFunc = (parId, renderCallBack)=> {
    async.parallel({
        parProductList: (callBack)=> {
            httpOperate.httpPost('/pc/productInfoCtrl/getListByPar', {
                'parId': parId,
                'curPage': 1,
                'pageSize': 10
            }, (result)=> {
                try {
                    var resultMessage = getListByparId.getListByParIdDataOperate(result);
                    store.dispatch(actions.receiveParMessage({
                        name: resultMessage.name,
                        parId: resultMessage.parId,
                        profit: resultMessage.profit,
                        financingAmount: resultMessage.financingAmount,
                        surplusAmount: resultMessage.surplusAmount,
                        process: resultMessage.process,
                        time: resultMessage.time,
                        riskLevel: result.result[0].riskLevel,
                        proDesc: result.result[0].proDesc
                    }));
                    store.dispatch(actions.receiveParProduct({
                        size: Math.ceil(result.result[0].size / 10),
                        current: 1,
                        product: resultMessage.products
                    }));
                }
                catch (err) {
                    logger.error(err);
                }
                finally {
                    callBack();
                }
            })
        }

    }, (err, results)=> {
        if (renderCallBack) {
            var modelRender = new ModelRender();
            var main = reactDom.renderToString(
                <Provider store={store}>
                    <HkfsProductDetail/>
                </Provider>
            );
            var initializeStore = JSON.stringify(store.getState());
            renderCallBack(modelRender.headerRender(), modelRender.barRender(), modelRender.footerRender(), main, initializeStore);
        }
    });


};